package cn.com.api.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.api.entity.ApiU_advert_position;

import cn.com.api.service.ApiU_advert_positionService;

import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_API + "u_advert_position/")
public class ApiU_advert_positionController {
    @Autowired
    private ApiU_advert_positionService apiU_advert_positionService;
	
	private static Logger logger = Logger.getLogger(ApiU_advert_positionController.class);
	
	/**
	 * 新增
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "insertU_advert_position")
	public void insertU_advert_position(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String position_name = request.getParameter("position_name");
		String pre_picture = request.getParameter("pre_picture");
		String unit_price = request.getParameter("unit_price");
		String unit = request.getParameter("unit");
		String spec = request.getParameter("spec");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		String pre_prcture_inn = request.getParameter("pre_prcture_inn");
		String spec_inn = request.getParameter("spec_inn");
        
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_advert_position u_advert_position = new ApiU_advert_position();
				
    		    //u_advert_position.setId(id);
    		    u_advert_position.setPosition_name(position_name);
    		    u_advert_position.setPre_picture(pre_picture);
    		    //u_advert_position.setUnit_price(unit_price);
    		    //u_advert_position.setUnit(unit);
    		    u_advert_position.setSpec(spec);
    		    u_advert_position.setExt1(ext1);
    		    u_advert_position.setExt2(ext2);
    		    u_advert_position.setExt3(ext3);
    		    u_advert_position.setExt4(ext4);
    		    u_advert_position.setExt5(ext5);
    		    u_advert_position.setPre_prcture_inn(pre_prcture_inn);
    		    u_advert_position.setSpec_inn(spec_inn);
				
				apiU_advert_positionService.insertU_advert_position(u_advert_position);
				// return
                map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "添加成功！");
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 根据id查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_advert_positionById")
	public void findU_advert_positionById(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    String id = request.getParameter("id");
		    ApiU_advert_position u_advert_position = apiU_advert_positionService.findU_advert_positionById(Integer.valueOf(id));
		
			// return
            map.put("datas", u_advert_position);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
     /**
	 * 查询数据集
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/findU_advert_positions")
	public void findU_advert_positions(HttpServletRequest request, HttpServletResponse response) throws Exception {
	
		Map<String,Object> map = new HashMap<String,Object>();
		try{
		    List<ApiU_advert_position> u_advert_positions = apiU_advert_positionService.findU_advert_positions();
		
			// return
            map.put("datas", u_advert_positions);
            map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "查询成功！");
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
        
	}
	
    
	/**
	 * 修改
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@Transactional
	@RequestMapping(value = "updateU_advert_position")
	public void updateU_advert_position(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String id = request.getParameter("id");
		String position_name = request.getParameter("position_name");
		String pre_picture = request.getParameter("pre_picture");
		String unit_price = request.getParameter("unit_price");
		String unit = request.getParameter("unit");
		String spec = request.getParameter("spec");
		String ext1 = request.getParameter("ext1");
		String ext2 = request.getParameter("ext2");
		String ext3 = request.getParameter("ext3");
		String ext4 = request.getParameter("ext4");
		String ext5 = request.getParameter("ext5");
		String pre_prcture_inn = request.getParameter("pre_prcture_inn");
		String spec_inn = request.getParameter("spec_inn");
        
        
		//String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
				ApiU_advert_position u_advert_position = new ApiU_advert_position();
				//获取原数据
				u_advert_position  = apiU_advert_positionService.findU_advert_positionById(Integer.valueOf(id));
				//修改
				//u_advert_position.setIs_open(Integer.valueOf(is_open));
				//
				int ret_i = apiU_advert_positionService.updateU_advert_position(u_advert_position);
				
				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
				map.put(Utils.MESSAGE, "编辑成功！");
			}else{
				map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				map.put(Utils.MESSAGE, "检查失败！");
			}
		}
		catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
    
	/**
	 * 删除
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value = "deleteU_advert_position")
	public void deleteU_advert_position(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
        String id = request.getParameter("id");
		Map<String,Object> map = new HashMap<String,Object>();
		
		try{
			if(id!=null && !"".equals(id.trim())){
	
				Integer ret_I = apiU_advert_positionService.deleteU_advert_position(Integer.parseInt(id));
				if(ret_I >0 ){
    				map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
    				map.put(Utils.MESSAGE, "删除成功！");
                }else{
				    map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
				    map.put(Utils.MESSAGE, "检查失败！");
			    }
			}
		}catch(Exception e){
			logger.error(e);
            e.printStackTrace();  
    		map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
    		map.put(Utils.MESSAGE, "系统异常！");
		}

		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}
	
 
}


